﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Manageweb_Member_ViewMemGuanXi : System.Web.UI.Page
{

    DataView dv;
    DataTable dt;

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            SQLExpressDAL.UsersTableAdapters.UsersTableAdapter userAdp = new SQLExpressDAL.UsersTableAdapters.UsersTableAdapter();
            SQLExpressDAL.Users.UsersDataTable userTable = userAdp.GetAllUsers();
            dt = (DataTable)userTable;
            //第一次加载时调用方法传参 
            CreateTree("admins", null, dt, this.TreeView1);
            this.TreeView1.ExpandAll();
        }
    }

    /// <summary> 
    /// 创建一个树 
    /// </summary> 
    /// <param name="parentID">父ID</param> 
    /// <param name="node">节点</param> 
    /// <param name="dt">DataTable</param> 
    /// <param name="treeView">TreeView的名称</param> 
    public void CreateTree(String parentID, TreeNode node, DataTable dt, TreeView treeView)
    {
        //实例化一个DataView dt = 传入的DataTable 
        dv = new DataView(dt);
        //筛选(相当于select * from tabMenu where menuParentID = 传入的 parentID) 
        dv.RowFilter = " [parentID]='" + parentID + "'";

        //用foreach遍历dv 
        foreach (DataRowView row in dv)
        {
            //第一次加载时为空 
            if (node == null)
            {
                //创建根节点 
                TreeNode root = new TreeNode();
                //必须与数据库的对应 
                root.Text = string.Format("{0}  {1}  {2}  {3}", row["userid"].ToString(), row["username"].ToString(), row["LjScore"].ToString(), row["SpecialScore"].ToString());
                root.Value = row["userid"].ToString();
                //添加根节点 
                this.TreeView1.Nodes.Add(root);
                //递归调用方法本身 
                CreateTree(row["userid"].ToString(), root, dt, treeView);

            }
            else
            {
                //添加子节点 
                TreeNode childNode = new TreeNode();
                childNode.Text = string.Format("{0}  {1}  {2}  {3}", row["userid"].ToString(), row["username"].ToString(), row["LjScore"].ToString(), row["SpecialScore"].ToString());
                childNode.Value = row["userid"].ToString();
                node.ChildNodes.Add(childNode);
                CreateTree(row["userid"].ToString(), childNode, dt, treeView);

            }
        }

    }

}
